/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.kiakaku.utility;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kiakaku.downloadpage.Node;

/*
 *   File FileUtility is created by kiakaku on Nov 2, 2011 9:24:10 AM
 * If you want modify this file please insert information in here 
 * - Last modify in Nov 2, 2011, 9:24:10 AM by kiakaku
 *                  
 *
 * @author kiakaku
 */
public class FileUtility {

    public static String docFile(String pathFile) {
        FileReader fr = null;
        BufferedReader reader = null;
        try {
            fr = new FileReader(pathFile);
            reader = new BufferedReader(fr);
            String line = "";
            StringBuilder str = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                str.append(line).append("\n");
            }
            fr.close();
            return str.toString();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                fr.close();
                reader.close();
                return null;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    public static boolean ghiListNode(List<Node> node, String path) {
        boolean ok = true;
        ObjectOutputStream oos = null;
        int i = 1;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(path));
            for (Node n : node) {
                oos.writeObject(n);
                System.out.println("Ghi node " + i + n.toString());
                i++;
            }
        } catch (IOException ex) {
            Logger.getLogger(FileUtility.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (oos != null) {
                    oos.flush();
                    oos.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(FileUtility.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return ok;
    }

    public static List<Node> docListNode(String path) {
        long abc = System.currentTimeMillis();
        List<Node> returnList = new LinkedList<Node>();
        ObjectInputStream ois = null;
        int i = 1;
        try {
            ois = new ObjectInputStream(new FileInputStream(path));
            Object obj = null;
            while ((obj = ois.readObject()) != null) {
                if (obj instanceof Node) {
                    returnList.add((Node) obj);
                    System.out.println("Đọc Node " + i + obj.toString());
                    i++;
                }
            }

        } catch (EOFException ex) { //This exception will be caught when EOF is reached
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the ObjectInputStream
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return returnList;
    }

    public static boolean ghiListOne(List<Node> node, String path) {
        boolean ok = true;
        ObjectOutputStream oos = null;
        int i = 1;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(path));
            oos.writeObject(node);
            i++;
        } catch (IOException ex) {
            Logger.getLogger(FileUtility.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (oos != null) {
                    oos.flush();
                    oos.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(FileUtility.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return ok;
    }

    public static boolean ghiListInt(List<Integer> node, String path) {
        boolean ok = true;
        ObjectOutputStream oos = null;
        int i = 1;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(path));
            oos.writeObject(node);
            i++;
        } catch (IOException ex) {
            Logger.getLogger(FileUtility.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (oos != null) {
                    oos.flush();
                    oos.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(FileUtility.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return ok;
    }

    public static List<Integer> docListInt(String path) {
        List<Integer> returnList = new LinkedList<Integer>();
        ObjectInputStream ois = null;
        int i = 1;

        try {
            ois = new ObjectInputStream(new FileInputStream(path));
            Object obj = null;
            while ((obj = ois.readObject()) != null) {
                returnList = (LinkedList<Integer>) obj;
            }

        } catch (EOFException ex) {
            System.out.println("Đọc xong");
        } catch (Exception ex) {
            System.out.println("Đọc xong");
        } finally {
            //Close the ObjectInputStream
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return returnList;
    }

    public static List<Node> docListOne(String path) {
        long abc = System.currentTimeMillis();
        List<Node> returnList = new LinkedList<Node>();
        ObjectInputStream ois = null;
        int i = 1;

        try {
            ois = new ObjectInputStream(new FileInputStream(path));
            Object obj = null;
            while ((obj = ois.readObject()) != null) {
                returnList = (LinkedList<Node>) obj;
            }

        } catch (EOFException ex) { //This exception will be caught when EOF is reached
            long def = System.currentTimeMillis();
            System.out.println("doc list one het: " + (def - abc));
            System.out.println("End of file reached.");
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            //Close the ObjectInputStream
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return returnList;
    }

    public static boolean ghiFile(String content, String path) {
        BufferedWriter out = null;
        FileWriter fstream = null;

        try {
            fstream = new FileWriter(path);
            out = new BufferedWriter(fstream);
            out.write(content);
            out.close();
            return true;
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            return false;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ex) {
                    Logger.getLogger(FileUtility.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void main(String[] args) {
//        System.out.println(FileUtility.docListOne("list_all_node.txt").size());
        for (Node a : FileUtility.docListOne("list_all_node.txt")) {
            System.out.println(a.getUrl() + "\t\ttid:" + a.getId());

        }
    }
}
